<?php
/**
 * 结算典型问题库
 * @author kongkai <kongkai@88.com>
 * @date 2022-04-17
 */

namespace app\app\controller;

use app\app\model\ProjectModel;
use app\app\model\RandModel;
use app\app\model\SettlementCaseModel;
use app\BaseController;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class SettlementCase extends BaseController
{
    private $model;

    /**
     * initialize
     * @return void
     */
    public function initialize()
    {
        $this->model = SettlementCaseModel::class;
    }

    /**
     * C&U
     * @return void
     */
    public function save()
    {
        $id = input('id', '');
        $param = $this->request->param();

        if (empty($id)) {
            $model = new $this->model();
        } else {
            $model = $this->model::where('id', $id);
        }

        $model->save($param);

        $this->success('success');
    }

    /**
     * R
     * @return void
     * @throws DbException
     */
    public function list()
    {
        $where = $this->request->param();
        unset($where['page'], $where['pageSize']);
        $where = array_filter($where);
//        $where[] = [1, 1];

        $page_size = input('pageSize', '20');

        $data = $this->model::where($where)->paginate($page_size);

        empty($data) && $this->error('查询为空');
        $data = $data->toArray();
        empty($data['data']) && $this->error('未找到符合条件的数据');

        $this->success('success', $data);
    }

    /**
     * detail
     * @param $id
     * @return void
     */
    public function detail($id = 0)
    {
        !$id && $this->error('请传入id');

        $data = $this->model::where('id', $id)->find();
        empty($data) && $this->error('查询为空');

        $this->success('success', $data);
    }

    /**
     * D
     * @return void
     * @throws DbException
     */
    public function del($id = 0)
    {
        !$id && $this->error('请传入id');

        $this->model::where('id', $id)->delete();

        $this->success('success');
    }
}
